var mongoose = require('mongoose');
const path = require('path');
mongoose.Promise = global.Promise;
const winston = require('../utils/winstonLogger.js');

//connect to mongodb
mongoose.set('debug',true);
mongoose.connect(
    'mongodb://localhost:27017/test',
    {useMongoClient:true}
).then(function(db){
    winston.info('DB connect success !!')
}).catch(function(err){
    winston.error('DB connect failed!!');
    winston.error(err.stack);
});

var merchantModel = require('../models/Merchant.js');


var merchants =[{
    'username': 'wyatt',
    'password': '123',
    //merchant name, use for show
    'name':'旺仔',
    'idCard': path.join(__dirname, '../../public/idCard'),
    'portrait':path.join(__dirname, '../../public/portrait'),
    'address': '海怡湾畔',
    'status':'0',
    'foods': [],
    'orderInfos': []
},{
    'username': 'wyatt1',
    'password': '123',
    //merchant name, use for show
    'name':'旺仔1',
    'idCard': path.join(__dirname, '../../public/idCard'),
    'portrait':path.join(__dirname, '../../public/portrait'),
    'address': '海怡湾畔',
    'status':'0',
    'foods': [],
    'orderInfos': []
}]
var merchantDao ={};
merchantDao.add = function (merchants) {
    merchantModel.insertMany(merchants,function(error,docs){
        if(error)
            return winston.error(error);
        else
            console.log(docs);
    })
}

merchantDao.delete = function (merchantId) {
    merchantModel.deleteMany({_id:merchantId},function(error,docs){
        if(error)
            return winston.error(error);
        else
            console.log(docs);

    })
}

var newMerchant = {
    _id:'59a8cea6fd6ebc162818ba15',
    'username': 'marvin',
    'password': '123',
    //merchant name, use for show
    'name':'marvin',
    'idCard': path.join(__dirname, '../../public/idCard'),
    'portrait':path.join(__dirname, '../../public/portrait'),
    'address': '海怡湾畔',
    'status':'0',
    'foods': [],
    'orderInfos': []
}
merchantDao.update = function (newMerchant) {
    merchantModel.updateMany({_id:newMerchant._id},newMerchant,function(error,docs){
        if(error)
            return winston.error(error);
        else
            console.log(docs);
    })
}

merchantDao.query = function($n={}){

    merchantModel.find($n,function(error,docs){
        if(error)
            return winston.error(error);
        else
            console.log(docs);
    });
}
//merchantDao.add(merchants);
//merchantDao.delete("59a8cea6fd6ebc162818ba14");
//merchantDao.update(newMerchant);
//merchantDao.query({_id:'59a8cea6fd6ebc162818ba15'});